package com.yonyou.tj.service;

import com.yonyou.tj.mapper.SignResultMapper;
import com.yonyou.ucf.mdd.ext.dao.meta.MetaDaoHelper;
import lombok.extern.slf4j.Slf4j;
import org.imeta.orm.schema.QueryCondition;
import org.imeta.orm.schema.QuerySchema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class SignService {

    @Autowired
    private SignResultMapper signResultMapper;

    public String getSignResultByInstId(Map<String, Object> params) throws Exception {
        String instid = params.get("instid") == null?null:String.valueOf(params.get("instid"));
        String signName = params.get("signname") == null?null:String.valueOf(params.get("signname"));
        // 默认30s 优先使用接口传入数据
        Integer timegap = params.get("timegap") == null?30:Integer.parseInt(String.valueOf(params.get("timegap")));

        Assert.notNull(instid,"instid不能为空！！！");
        Assert.notNull(signName,"signname不能为空！！！");

       List<Map<String, Object>> res = signResultMapper.getDeleteReasonByInstId(instid, signName);
        String signResult = "3"; // 默认都不通过
        if(res == null || res.size() == 0 ) {
            return signResult;
        }
        log.error("根据instid{},signname{},timegap{}查询的会签内容：{}", instid, signName,timegap, res);

        boolean isCompleted = false;
        boolean isReject = false;

        Date standDate = null;
        boolean isSame = true;
        for(int i=0; i< res.size(); i++) {
            Map<String, Object> resMap = res.get(i);
            String approval_action_ = resMap.get("approval_action_")+"";

            if(standDate == null) {
                standDate = (Date) resMap.get("start_time_");
            } else {// 开始时间相差 大于 传入时间，视作 不同活动任务数据，不进行处理
                isSame = isSameDate(standDate, (Date) resMap.get("start_time_"), timegap);
            }
            if(!isSame) {// 若时间不一致，则跳过，后续数据不进行判断
                break;
            }
            // 状态为 已完成 审批动作为 同意
            if("completed".equals(resMap.get("delete_reason_"))
                    && ("counterSignUserTaskAgree,rejectAction:notApproved-reject".equals(approval_action_)
                        || "agree".equals(approval_action_))) {
                isCompleted = true;
            } else {
                isReject = true;
            }
        }
        if(isCompleted && isReject) {
            signResult = "2"; // 部分同意
        }
        if(isCompleted && !isReject) {
            signResult = "1"; // 全部同意
        }
        if(!isCompleted && isReject) {
            signResult = "3"; // 全部不同意
        }
        return signResult;
    }

    /**
     * 判断两个日期时间是否在传入的时间范围内
     * @param standDate
     * @param start_time_
     * @param timegap
     * @return
     */
    private boolean isSameDate(Date standDate, Date start_time_, Integer timegap) {
        long time = standDate.getTime();
        long time1 = start_time_.getTime();
        long t = Math.abs(time - time1);
        if(t <= timegap*1000) {
            return true;
        }
        return false;
    }

}
